// backend/src/routes/ueInstanceRoutes.js
const ueInstanceManager = require("../modules/ueInstance/index.js");

async function ueInstanceRoutes(fastify, options) {
  // 获取实例列表
  fastify.get("/ueinstance/list", async (request, reply) => {
    try {
      const result = await ueInstanceManager.getAllInstances();
      return result;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });

  // 创建新实例
  fastify.post("/ueinstance/create", async (request, reply) => {
    try {
      const instanceData = request.body;
      const result = await ueInstanceManager.createInstance(instanceData);
      return result;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });

  // 删除实例
  fastify.post("/ueinstance/delete", async (request, reply) => {
    try {
      const { instanceId } = request.body;
      if (!instanceId) {
        return reply.code(400).send({ error: "实例ID不能为空" });
      }
      const result = await ueInstanceManager.deleteInstance(instanceId);
      return result;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });

  // 启动实例
  fastify.post("/ueinstance/start", async (request, reply) => {
    try {
      const { instanceId } = request.body;
      if (!instanceId) {
        return reply.code(400).send({ error: "实例ID不能为空" });
      }
      const result = await ueInstanceManager.startInstance(instanceId);
      return result;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });

  // 停止实例
  fastify.post("/ueinstance/stop", async (request, reply) => {
    try {
      const { instanceId } = request.body;
      if (!instanceId) {
        return reply.code(400).send({ error: "实例ID不能为空" });
      }
      const result = await ueInstanceManager.stopInstance(instanceId);
      return result;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });
  // 重启实例
  fastify.post("/ueinstance/restart", async (request, reply) => {
    try {
      const { instanceId } = request.body;
      if (!instanceId) {
        return reply.code(400).send({ error: "实例ID不能为空" });
      }
      const result = await ueInstanceManager.restartInstance(instanceId);
      return result;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });
  // 获取可用实例
  fastify.get("/ueinstance/getAvailableInstances", async (request, reply) => {
    try {
      const result = await ueInstanceManager.getAvailableInstances();
      return result;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });
  // 获取基础配置
  fastify.get("/ueinstance/getConfig", async (request, reply) => {
    try {
      const result = await ueInstanceManager.getConfig();
      return result;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });
  fastify.post("/ueinstance/setConfig", async (request, reply) => {
    const newConfig = request.body;
    try {
      if (!newConfig || typeof newConfig !== "object") {
        return reply.code(400).send({ error: "配置数据不能为空且必须是对象" });
      }
      const result = await ueInstanceManager.setConfig(newConfig);
      return result;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });

  fastify.post("/ueinstance/msgBySignal", async (request, reply) => {
    const signalData = request.body;
    try {
      const result = await ueInstanceManager.msgBySignal(signalData);
      return result;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });
  fastify.post("/ueinstance/clearClient", async (request, reply) => {
    const { instanceId } = request.body;
    try {
      const result = await ueInstanceManager.clearClient(instanceId);
      return result;
    } catch (error) {
      return reply.code(500).send({ error: error.message });
    }
  });
}

module.exports = ueInstanceRoutes;
